﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleAppDemo.project {
    /// <summary>
    /// 大文件分割多个小文件
    /// </summary>
    public class BigFileManage {
        private string filePath;
        private string splitFileFormat;
        private int splitMinFileSize;
        private int splitFileSize;
        public BigFileManage(string filePath, string splitFileFormat, int splitMinFileSize, int splitFileSize)
        {
            this.filePath = filePath;
            this.splitFileFormat = splitFileFormat;
            this.splitFileSize = splitFileSize;
            this.splitMinFileSize = splitMinFileSize;
        }
        /// <summary>
        /// 实现方案1：一次性读取1M，直到读取到200M为止，开始写入下一个分割文件。
        /// </summary>
        public void Case01()
        {
            using (FileStream readerStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                using (BinaryReader reader = new BinaryReader(readerStream))
                {
                    int fileCursor = 0;
                    int readerCursor = 0;
                    char[] buffer = new char[1024 * 1024];
                    int length = 0;

                    NextFileBegin:
                    string filePath = string.Format(splitFileFormat, fileCursor);
                    Console.WriteLine("开始读取文件【{1}】：{0}", filePath, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                    using (FileStream writerStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        using (BinaryWriter writer = new BinaryWriter(writerStream))
                        {
                            while ((length = reader.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                readerCursor++;
                                writer.Write(buffer, 0, length);
                                if (readerCursor >= splitFileSize)
                                {
                                    Console.WriteLine("结束读取文件【{1}】：{0}", filePath, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                                    readerCursor = 0;
                                    fileCursor++;
                                    goto NextFileBegin;
                                }
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 实现方案2：一次性读取200M，立即写入分割文件，开始下一个分割文件操作。
        /// </summary>
        public void SplitFileSize()
        {
            FileInfo fileInfo = new FileInfo(filePath);
            if (fileInfo.Length > splitMinFileSize)
            {
                Console.WriteLine("判定结果：需要分隔文件！");
            }
            else
            {
                Console.WriteLine("判定结果：不需要分隔文件！");
                return;
            }

            int steps = (int)(fileInfo.Length / splitFileSize);
            using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                using (BinaryReader br = new BinaryReader(fs))
                {
                    int couter = 1;
                    bool isReadingComplete = false;
                    while (!isReadingComplete)
                    {
                        string filePath = string.Format(splitFileFormat, couter);
                        Console.WriteLine("开始读取文件【{1}】：{0}", filePath, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                        byte[] input = br.ReadBytes(splitFileSize);
                        using (FileStream writeFs = new FileStream(filePath, FileMode.Create))
                        {
                            using (BinaryWriter bw = new BinaryWriter(writeFs))
                            {
                                bw.Write(input);
                            }
                        }

                        isReadingComplete = (input.Length != splitFileSize);
                        if (!isReadingComplete)
                        {
                            couter += 1;
                        }
                        Console.WriteLine("完成读取文件【{ 1}】：{ 0}", filePath, DateTime.Now.ToString("yyyy - MM - dd HH: mm: ss.fff"));
                    }
                }
            }
            Console.WriteLine("分隔完成，请按下任意键结束操作。。。");
        }
    }
}
